﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using MossmanMill.View;

namespace MossmanMill.DataModel
{
    public class Sensor : IDrawable
    {
        public int Id { get; private set; } //Sensor_Id
        public int CardId { get; private set; } //Card_Id
        public int Channel { get; private set; } //Sensor_Channel
        public string Tag { get; set; } //Sensor_Tag
        public string Param { get; set; } //Sensor_Param
        public string Desc { get; set; } //Sensor_Desc

        private Card _card;
        public Card Card
        {
            get {
                if (_card != null) return _card;
                var db = DatabaseAccess.Instance;
                _card = db.GetCard(CardId);
                return _card;
            }
        }

        public Sensor(int id, string tag, string param, string desc, int channel, int cardId)
        {
            Id = id;
            Tag = tag;
            Param = param;
            if (Param.StartsWith(Tag)) Param = Param.Substring(Tag.Length);
            if (Param.StartsWith(".")) Param = Param.Substring(1);
            Desc = desc;
            Channel = channel;
            CardId = cardId;
        }

        public bool IsBlank()
        {
            return Tag.Length == 0 && Param.Length == 0 && (Desc.Length == 0 || Desc.Equals("Spare"));
        }

        public override string ToString()
        {
            return Tag;
        }

        public void Release()
        {
            _card = null;
        }

        private bool hover; //if mouse is hovering over

        public void draw(Graphics graphics, int x, int y, float scale = 1.0f, bool drawChildren = false)
        {
            draw(graphics, new Rectangle(x, y, 261, 121), scale, drawChildren);
        }

        public void draw(System.Drawing.Graphics graphics, Rectangle bounds, float scale = 1.0f, bool drawChildren = false)
        {
            var x = bounds.Left;
            var y = bounds.Top;
            var buffer = new Bitmap(bounds.Width, bounds.Height);
            var g = Graphics.FromImage(buffer);
            Point[] points = new Point[]{
                    new Point(0,30),
                    new Point(160,30),
                    new Point(160,110),
                    new Point(20,110),
                    new Point(0,90),
                    new Point(0,30)
                };
            g.FillPolygon(Brushes.LightGray, points);
            g.DrawPolygon(Pens.Blue, points);
            g.DrawString(Tag, Fonts.smallCourier, Brushes.Black, new PointF(4, 38));
            g.DrawString(Param, Fonts.smallCourier, Brushes.Black, new PointF(4, 54));
            if (hover)
            {
                Drawing.FillOutlineRectangle(g, new Rectangle(100, 0, 160, 50), Brushes.LightGoldenrodYellow, Pens.Black);
                g.DrawString(Desc, Fonts.smallCourier, Brushes.Black, new Rectangle(100, 0, 160, 50));
            }
            graphics.DrawImage(buffer, x, y, buffer.Width * scale, buffer.Height * scale);
        }

        public bool interact(System.Drawing.Point location, int type, int x, int y, float scale = 1.0f, bool interactWithChildren = false) //event location, event type, draw x, draw y
        {
            bool h = hover;
            if (type == 1)
            {
                hover = new Rectangle(x, y, 160, 80).Contains(location);
            }
            return h != hover;
        }

        public bool Equals(Sensor sensor)
        {
            bool equal = sensor.Channel == Channel && 
                sensor.Tag.Equals(Tag) &&
                sensor.Param.Equals(Param) &&
                sensor.Desc.Equals(Desc);
            if (!equal)
            {
                Console.WriteLine(sensor.Channel + " - " + Channel);
                Console.WriteLine(sensor.Tag + " - " + Tag);
                Console.WriteLine(sensor.Param + " - " + Param);
                Console.WriteLine(sensor.Desc + " - " + Desc);
            }
            return equal;
        }
    }
}
